<template>
<div>
    <el-dialog title="用户注册" :visible.sync="modallogin" :closable="true" :mask-closable="false" style="width: 50%;margin-left: 25%">
        <p slot="header" style="color:#84a1ff;text-align:center">
            <span>用户注册</span>
        </p>
        <el-form :model="ruleForm1" :rules="rules1" ref="ruleForm1" label-position="left" label-width="20%">
            <el-form-item prop="account" style="width: 70%; margin-left: 10%" auto-complete="off" label="账号">
                <el-input type="text" v-model="ruleForm1.account" auto-complete="off" placeholder="账号">
                </el-input>
            </el-form-item>
            <el-form-item prop="nickname" style="width: 70%;margin-left: 10%" label="昵称">
                <el-input type="text" v-model="ruleForm1.nickname" placeholder="昵称"></el-input>
            </el-form-item>
            <el-form-item prop="checkPass" style="width: 70%;margin-left: 10%" label="密码">
                <el-input type="password" v-model="ruleForm1.checkPass" auto-complete="off" placeholder="密码"></el-input>
            </el-form-item>
            <el-form-item prop="checkPass1" style="width: 70%;margin-left: 10%" label="重复密码">
                <el-input type="password" v-model="ruleForm1.checkPass1" auto-complete="off" placeholder="再次输入密码"></el-input>
            </el-form-item>
            <el-form-item prop="email" style="width: 70%;margin-left: 10%" label="邮箱">
                <el-input type="text" v-model="ruleForm1.email" auto-complete="off" placeholder="邮箱">
                </el-input>
            </el-form-item>
            <el-form-item prop="verify" style="width:70%;margin-left: 10%" label="验证码">
                <el-input type="text" style="width: 50%;float: left;font-size: 8px" v-model="ruleForm1.verify" auto-complete="off" placeholder="验证码"></el-input>
                <el-button type="primary" style="margin-left:10%; font-size:7px;width:39%;" @click.native.prevent="handleVerify" :loading="logining">发送验证码</el-button>
            </el-form-item>
            <el-form-item style="width:30%;margin-left: 35%">
                <el-button type="primary" @click.native.prevent="handleRegister" :loading="logining">注册</el-button>
            </el-form-item>
        </el-form>
        <div slot="footer"></div>
    </el-dialog>
    <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" label-position="left" label-width="0px" class="demo-ruleForm login-container">
        <h3 class="title">系统登录</h3>
        <el-form-item prop="account">
            <el-input type="text" v-model="ruleForm2.account" auto-complete="off" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="checkPass">
            <el-input type="password" v-model="ruleForm2.checkPass" auto-complete="off" placeholder="密码"></el-input>
        </el-form-item>
        <el-checkbox v-model="checked" checked class="remember" style="float: left;">记住密码</el-checkbox>
        <el-form-item style="width:100%;">
            <el-button type="primary" style="width:100%;" @click.native.prevent="handleSubmit2" :loading="logining">登录</el-button>
        </el-form-item>
        <el-form-item style="width:100%;height: 10px">
            <a style="float: right" @click="modallogin = true">注册</a>
        </el-form-item>
    </el-form>
</div>
</template>

<script>
import ajax from "../api/ajax";
import store from "../store/index";
import Cookie from "js-cookie";
import Const from "@/const.js";
import Message from 'element-ui/lib/message'
export default {
    name: "login",
    store: store,
    data() {
        const validateEmail = (rule, value, callback) => {
            if (value === "") {
                console.log("value is  " + value);
                callback("请输入邮箱");
            } else {
                let reg = new RegExp(
                    "^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"
                );
                if (!reg.test(value)) {
                    callback("邮箱格式错误");
                } else {
                    callback();
                }
            }
        };
        const validatePassword = (rule, value, callback) => {
            if (value === "") {
                callback("请再次输入密码");
            } else {
                if (typeof value == "string") {
                    let pattern = /^[\w_-]{6,16}$/;
                    if (pattern.test(value)) {
                        callback();
                    } else {
                        callback("密码不合规最短6位，最长16位,");
                    }
                }
            }
        };
        return {
            validatePassCheck: (rule, value, callback) => {
                if (value === "") {
                    callback(new Error("请再次输入密码"));
                } else if (value !== this.formCustom.passwd) {
                    callback(new Error("两次输入密码不一致!"));
                } else {
                    callback();
                }
            },
            modallogin: false,
            logining: false,
            ruleForm1: {
                account: "",
                checkPass: "",
                checkPass1: "",
                verify: "",
                email: "",
                nickname: ""
            },
            ruleForm2: {
                account: "admin",
                checkPass: "123456"
            },
            rules1: {
                account: [{
                        required: true,
                        message: "请输入账号",
                        min: 6,
                        trigger: "blur"
                    }
                    //{ validator: validaePass }
                ],
                nickname: [{
                        required: true,
                        message: "请输入昵称",
                        min: 6,
                        trigger: "blur"
                    }
                    //{ validator: validaePass }
                ],
                checkPass: [{
                    validator: validatePassword,
                    trigger: "blur"
                }],
                checkPass1: [{
                    validator: validatePassword,
                    trigger: "blur"
                }],
                email: [{
                    validator: validateEmail,
                    trigger: "blur"
                }]
            },
            rules2: {
                account: [{
                        required: true,
                        message: "请输入账号",
                        trigger: "blur"
                    }
                    //{ validator: validaePass }
                ],
                checkPass: [{
                        required: true,
                        message: "请输入密码",
                        trigger: "blur"
                    }
                    //{ validator: validaePass2 }
                ]
            },
            checked: true
        };
    },
    computed: {},
    methods: {
        handleReset2() {
            this.$refs.ruleForm2.resetFields();
        },
        handleVerify() {
            this.$refs.ruleForm1.validate(valid => {
                if (valid) {
                    let url = "/verify";
                    ajax
                        .post(url, {
                            email_address: this.ruleForm1.email
                        })
                        .then(resp => {
                            Message("ok");
                        })
                        .catch(reason => {
                            Message("网络错误:" + reason);
                        });
                }
            });
        },
        handleRegister() {
            this.$refs.ruleForm1.validate(valid => {
                if (valid) {
                    let url = "/register";
                    ajax
                        .post(url, {
                            remember_me: this.ruleForm1.checked ? 1 : 0,
                            user_name: this.ruleForm1.account,
                            user_pwd: this.ruleForm1.checkPass,
                            email_address: this.ruleForm1.email,
                            email_code: this.ruleForm1.verify
                        })
                        .then(resp => {
                            Message.info("网络错误:" + resp.data.Msg);
                        })
                        .catch(reason => {
                            Message.error("网络错误:" + reason);
                        });
                }
            });
        },
        handleSubmit2() {
            this.$refs.ruleForm2.validate(valid => {
                if (valid) {
                    let url = "/login";
                    ajax
                        .post(url, {
                            remember_me: this.ruleForm2.checked ? 1 : 0,
                            user_name: this.ruleForm2.account,
                            user_pwd: this.ruleForm2.checkPass
                        })
                        .then(resp => {
                            console.log(store);
                            if (resp.data.Status === 0) {
                                let getUserUrl = "/user?";
                                let objUser = JSON.parse(resp.data.Data);
                                console.log(objUser)
                                getUserUrl += "token=" + objUser.SocketToken;
                                getUserUrl += "&userid=" + objUser.ID;
                                ajax.get(getUserUrl).then(resp => {
                                        if (resp.data.Data !== undefined) {
                                            Cookie.set(Const.KEY_USER_INFO, resp.data.Data);
                                            store.commit("setAccount", resp.data.Data);
                                            console.log(resp.data.Data);
                                            this.$router.push("/BarIview");
                                            Message.success("登錄成功");
                                        } else {
                                            Message("网络错误");
                                        }
                                    })
                                    .catch(reason => {
                                        Message(reason);
                                    });
                            }
                        })
                        .catch(reason => {
                            console.log(reason);
                        });
                }
            });
        }
    },
    created() {}
};
</script>

<style>
.login-container {
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
}

.title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
}

.remember {
    margin: 0px 0px 35px 0px;
}
</style>
